package com.md.youyouhui.ui.user;

import android.databinding.DataBindingUtil;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.md.youyouhui.R;
import com.md.youyouhui.databinding.ActivityShoppingOrderinfoBinding;
import com.md.youyouhui.databinding.AdapterShoppingOrderinfoBinding;
import com.md.youyouhui.net.response.OrderListResponse;
import com.md.youyouhui.ui.BaseActivity;
import com.md.youyouhui.ui.adapter.DataBoundViewHolder;
import com.md.youyouhui.utils.GlideApp;
import com.md.youyouhui.utils.ToastUtils;
import com.md.youyouhui.view.MainViewModel;

import java.util.List;

/**
 * Created by AdministratorZJ
 * on 2019/5/17.
 */
public class OrderInfoShoppingActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener {
    private ActivityShoppingOrderinfoBinding infoBinding;
    private MyAdapter adapter;
    private MainViewModel viewModel;
    private int position;

    @Override
    public void setContentLayout() {
        infoBinding = DataBindingUtil.setContentView(this, R.layout.activity_shopping_orderinfo);
        viewModel = new MainViewModel();
    }

    @Override
    public void initView() {
        String title = getIntent().getStringExtra("title");
        infoBinding.toolbar.setTitle(title);
        setSupportActionBar(infoBinding.toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    @Override
    public void initData() {
        adapter = new MyAdapter();
        infoBinding.orderRecycler.setAdapter(adapter);
        infoBinding.orderGroup.setOnCheckedChangeListener(this);
        position = getIntent().getIntExtra("type", 100);
        getOrderInfo(position, "0");
    }

    private void getOrderInfo(int position, String type) {
        viewModel.getShoppingOrders(String.valueOf(position), type).observe(this, listResource -> {
            assert listResource != null;
            switch (listResource.status) {
                case LOADING:
                    infoBinding.progress.setVisibility(View.VISIBLE);
                    break;
                case SUCCESS:
                    infoBinding.progress.setVisibility(View.GONE);
                    List<OrderListResponse> data = listResource.data;
                    if (null == data) {
                        return;
                    }
                    if (data.size() == 0) {
                        Toast.makeText(ct, "暂无订单", Toast.LENGTH_SHORT).show();
                    }
                    adapter.upDate(data, Integer.parseInt(type));
                    break;
                case ERROR:
                    infoBinding.progress.setVisibility(View.GONE);
                    ToastUtils.errMake(this, listResource.errorCode);
                    break;
                default:
                    break;
            }
        });
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.orderYiUse:
                getOrderInfo(position, "0");
                break;
            case R.id.orderGoods:
                getOrderInfo(position, "1");
                break;
            case R.id.orderExpired:
                getOrderInfo(position, "2");
                break;
            default:
                break;
        }
    }

    class MyAdapter extends RecyclerView.Adapter<DataBoundViewHolder<AdapterShoppingOrderinfoBinding>> {
        List<OrderListResponse> rows;
        int type;

        public void upDate(List<OrderListResponse> rows, int type) {
            this.rows = rows;
            this.type = type;
            notifyDataSetChanged();
        }

        @NonNull
        @Override
        public DataBoundViewHolder<AdapterShoppingOrderinfoBinding> onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            AdapterShoppingOrderinfoBinding userBinding = DataBindingUtil.inflate(LayoutInflater.from(viewGroup.getContext()), R.layout.adapter_shopping_orderinfo, viewGroup, false);
            return new DataBoundViewHolder<>(userBinding);
        }

        @Override
        public void onBindViewHolder(@NonNull DataBoundViewHolder<AdapterShoppingOrderinfoBinding> holder, int i) {
            if (type == 1) {
                holder.binding.orderAdapterBtn.setVisibility(View.VISIBLE);
            } else {
                holder.binding.orderAdapterBtn.setVisibility(View.GONE);
            }
            holder.binding.orderAdapterContent.setText(rows.get(i).title);
            holder.binding.orderAdapterTime.setText(rows.get(i).createTime);
            holder.binding.orderAdapterPrice.setText(String.format("￥%s", rows.get(i).payAmount));
            GlideApp.with(ct).load(rows.get(i).cover).centerCrop().into(holder.binding.orderAdapterIv);
            holder.binding.orderAdapterBtn.setOnClickListener(v -> confirmOrder(rows.get(i).id));
        }

        @Override
        public int getItemCount() {
            return null == rows ? 0 : rows.size();
        }
    }

    private void confirmOrder(String id) {
        viewModel.orderConfrim(String.valueOf(position), id).observe(this, objectResource -> {
            assert objectResource != null;
            switch (objectResource.status) {
                case LOADING:
                    infoBinding.progress.setVisibility(View.VISIBLE);
                    break;
                case SUCCESS:
                    infoBinding.progress.setVisibility(View.GONE);
                    getOrderInfo(position, "1");
                    break;
                case ERROR:
                    infoBinding.progress.setVisibility(View.GONE);
                    ToastUtils.errMake(activity, objectResource.errorCode);
                    break;
                default:
                    break;
            }
        });
    }
}
